package com.kyle.pagingdemo.viewmodel

import androidx.lifecycle.viewModelScope
import androidx.paging.*
import com.kyle.pagingdemo.data.model.Post
import com.kyle.pagingdemo.paging.PostRepository
import com.kyle.pagingdemo.viewmodel.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

/**
 * author: liliang
 * date: 1/6/21 10:08 PM
 * desc:
 */
@HiltViewModel
class PagingViewModel @Inject constructor(private val postRepository: PostRepository) :
    BaseViewModel() {

    private lateinit var _postFlow: Flow<PagingData<Post>>

    val postFlow: Flow<PagingData<Post>>
        get() = _postFlow

    init {
        getPosts()
    }


    private fun getPosts() = launchPagingAsync({
               postRepository.getPosts().cachedIn(viewModelScope)
    },{
        _postFlow = it
    })

}